<template>
  <el-tree
    ref="elTree"
    style="margin:0"
    class="common-el-tree"
    :data="options"
    :props="{ children: 'children', label: 'name'}"
    node-key="id"
    @node-click="handleNodeClick" />
</template>
<script>
export default {
  components: { },
  props: {
    code: { type: String, required: true }
  },
  data() {
    return {
      resultVal: '',
      options: []
    }
  },
  watch: {

  },
  mounted() {
    this.initData()
  },
  created() {
  },
  methods: {

    async initData() {
      try {
        const response = await this.$store.dispatch('upmsCategoryDictTreeByType', this.code)
        this.options = response
        if (response.length > 0) {
          this.handleNodeClick(response[0], null)
        }
      } catch (_) {
        console.log()
      }
    },
    selectedInfo(result) {
      this.resultVal = result.id + ':' + result.name
    },
    handleNodeClick(data, node) {
      console.log(data, node)
      var fullPathId = null
      var fullPathName = null
      if (node === null) {
        fullPathName = data['name']
        fullPathId = data.id
      } else {
        if (node.parent.parent === null) {
          fullPathName = data['name']
          fullPathId = data.id
        } else {
          this.fullPathId = []
          this.fullPathName = []
          this.fullPathId.push(data.id)
          this.fullPathName.push(data['name'])
          this.getParentIds(node)
          fullPathName = this.fullPathName.reverse().join('/')
          fullPathId = this.fullPathId.reverse().join(',')
        }
      }

      this.selectedInfo = {
        id: data.id,
        name: data['name'],
        fullPathName: fullPathName,
        fullPathId: fullPathId
      }
      console.log(this.selectedInfo)
      this.$emit('selected', this.selectedInfo)
    },
    getParentIds(node) {
      var parentData = node.parent.data
      if (node.parent.parent === null) {
        if (parentData.id) {
          this.fullPathId.push(parentData.id)
          this.fullPathName.push(parentData['name'])
        }
      } else {
        this.fullPathId.push(parentData.id)
        this.fullPathName.push(parentData['name'])
        this.getParentIds(node.parent)
      }
    }
  }
}
</script>
<style lang="scss">
</style>
